import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

import forceLogin from '@/components/modal/force-login.vue';
import shareWechat from '@/components/share/share-wechat.vue';
import detailOpelist from '@/components/modal/detail-opelist.vue';
import { mapMutations, mapActions, mapState } from 'vuex';
export default {
	components: {
		forceLogin,
		shareWechat,
		detailOpelist
	},
	mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件) 
	data() {
		return {
			showTestDom:false,
			showStyle:2,
			advList:[],//顶部广告
			navList: [], //导航列表
			swiperHeight:'0',
			newsTabs: [], //新闻标签筛选
			oldtabIndex:0,
			tabIndex:0, //tab索引 
			newsList:[], //新闻列表
			downOption:{
				auto:false
			},
			upOption:{
				auto:false,
				page:{
					num:0,
					size :10,
					noMoreSize:0
				},
				empty:{
					use:false
				}
			},
			shareInfo:{
				
			},
			loginStatus:false,
		}
	},
	methods: {
		loginCallback(){
			var that = this;
			this.loginStatus = true;
			this.downCallback();
			this.openTest();
		},
		/*下拉刷新的回调 */
		downCallback() {
			
			// 这里加载你想下拉刷新的数据, 比如刷新轮播数据
			this.getIndexInfo();
		},
		/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
		upCallback() {
			this.getNewsList();
		},
		//获取新闻列表
		getNewsList(){
			var that = this;
			if(that.loginStatus == false)
			{
				that.mescroll.endSuccess(that.upOption.page.size,false);return ;
			}
			var info = this.newsTabs[that.tabIndex];
			var current_page = info.current_page?info.current_page:0;
			var last_page = info.last_page?info.last_page:1;
			if(current_page == last_page)
			{
				that.mescroll.endSuccess(that.upOption.page.size,false);
				that.mescroll.showNoMore();return ;
			}
			var localList = info.list?info.list:[];
			var getPage = current_page + 1; //要请求第几页的数据
			that.$http.get('Article/articleList',{params:{
				tab_id:info.id,
				page:getPage,
				per_page:that.upOption.page.size
			}}).then(res=>{
				if(res.code == 1)
				{
				 
					var resData = res.data;
					var newsList = localList.concat(resData.data); 
					that.newsList = newsList;
					//把每个下面的数据保存给tab
					info.current_page = resData.current_page;
					info.last_page = resData.last_page;
					info.list = newsList;
					that.newsTabs[that.tabIndex] = info;
					//重置状态
					setTimeout(()=>{
						var haveMore = (info.current_page == info.last_page || resData.data.length == 0)?false:true;
						that.mescroll.endSuccess(resData.data.length,haveMore)
						if(haveMore == false)
						{ 
							that.mescroll.showNoMore();
						}
					},20)
				}else{
					that.newsList = [];
					that.mescroll.endErr();
				}
			}).catch(res=>{
				that.mescroll.endErr();
			});
		},
		// 切换菜单
		tabChange (index) {
			this.loginStatus = this.$api.checkLogin(true)
			if(this.loginStatus == false)
			{
				return ;
			}
			var info = this.newsTabs[index];
			if(!info || index == this.oldtabIndex)
			{
				return ;
			}
			this.playVideoIndex = -1; //清空播放的视频
			this.oldtabIndex = index;
			this.mescroll.scrollTo(0); //跳转回顶部
			//处理文章数据
			if(info.list && info.list.length >= 0)
			{
				this.newsList = info.list;
				var current_page = info.current_page?info.current_page:0;
				var last_page = info.last_page?info.last_page:1;
				if(current_page == last_page)
				{
					//如果切换的时候 这个tab没更多了 就直接显示没更多内容了
					this.mescroll.showNoMore();
					return ;
				}
				this.mescroll.endSuccess(this.upOption.page.size,true); //否则话设置为还有下一页 
			}else{
				this.getNewsList();
			}
		},
		//获取首页数据
		getIndexInfo(){
			var that = this;
			that.$http.get('Article/index').then(res=>{
				if(res.code == 1)
				{
					var resData = res.data;
					that.advList = resData.advList;
					that.navList = resData.navList;
					that.newsTabs = resData.newsTabs;
					if(that.newsTabs.length > 0)
					{
						that.getNewsList();
					}
					if(resData.showTestDom)
					{
						that.showTestDom = resData.showTestDom;
					}
					
				}else{
					that.$api.msg(res.msg);
				}
			});
		},
	 
		shareFunc(index){
			var userInfo = this.$api.checkUserInfo();
			var user_id = userInfo.id?userInfo.id:0;
			var info = this.newsList[index];
			switch(info.b_type)
			{
				case 1:
					var path = '/pages/form/statistics/detail?s_id='+info.b_s_id+'&share_recom_userid='+user_id;
					break;
				case 2:
					var path = '/pages/form/daka/detail?s_id='+info.b_s_id+'&share_recom_userid='+user_id;
					break;	
				case 3:
					var path = '/pagesGoods/pages/form/goods/detail?s_id='+info.b_s_id+'&share_recom_userid='+user_id;
					break;		
			}
			
			this.shareInfo = {
				title:info.b_title,
				s_id:info.b_s_id,
				imageUrl:info.bg_image,
				path:path,
			}
			var posterData = { 
				headimg:info.c_avatar,
				nickname:info.c_nickname,
				create_time:info.b_createtime,
				bg_image:info.bg_image,
				title:info.b_title,
				qrcodeUrl:info.uni_qrcode,
				path:path,
			};
			this.$refs.share_wechat.setShowStatus(true,posterData);
		},
		shareClose(e){
			this.shareInfo = {};
		},
		showOpe(index){
			var that = this;
			var info = this.newsList[index];
			var data = {
				is_me:info.is_me,
				s_id:info.b_s_id,
				time_status:info.time_status,
				type:info.b_type,
				join_count:info.b_join_count,
				copy_rule_status:info.copy_rule_status
			};
			this.$refs.detail_opelist.initData(data,function(res){
				if(res.opeType == 'delete')
				{
					that.newsList.splice(index,1);
				}
				else if(res.opeType == 'setTime')
				{
					that.newsList[index].time_text = res.data.time_text;
					that.newsList[index].time_status = res.data.time_status;
					that.$forceUpdate();
				}
			});
		},
		
		openInfo(index){
			var info = this.newsList[index];
			console.log(info)
			if(info.b_type == 2)
			{
				var url = '/pages/form/daka/detail?s_id='+info.b_s_id;
			}else if(info.b_type == 3)
			{
				var url = '/pagesGoods/pages/form/goods/detail?s_id='+info.b_s_id;
			}else{
				var url = '/pages/form/statistics/detail?s_id='+info.b_s_id
			}
			this.$api.navigateTo(url);
		},
		//打开地址
		openurl:function(e){
			
			var dataset = e.currentTarget.dataset;
			console.log("url"+JSON.stringify(dataset))
			this.$api.navigateTo(dataset.url,dataset.url_type);
		},
		openTest()
		{
			var url = getApp().globalData.indexPage;
			if(url)
			{
				uni.navigateTo({
					url:url
				});
			}
		}
	},
	onLoad(params){
		this.$api.opeShareRecommUserId('set',params);
		/* this.$store.commit('showForceLogin',true);
		console.log(this.showForceLogin) */
		//this.getIndexInfo();
		
		
	},
	onShow(){
		this.loginStatus = this.$api.checkLogin(true)
		this.getIndexInfo();
		this.openTest();
		//this.$api.updateVersion();
	},
	onShareAppMessage(e)
	{
		var shareInfo = this.shareInfo;
		var title = shareInfo.title?shareInfo.title:'推荐您使用'+this.myconfig.site_name;
		var userInfo = this.$api.checkUserInfo();
		var user_id = userInfo.id?userInfo.id:0;
		if(shareInfo.path)
		{
			var path = shareInfo.path+'&share_recom_userid='+user_id;
		}else{
			var path = '/pages/index/index?share_recom_userid='+user_id;
		}
		var shareObj = {
			title:title,
			path:path
		}
		if(shareInfo.imageUrl)
		{
			shareObj.imageUrl = shareInfo.imageUrl;
		}
		return shareObj;
	},
	onShareTimeline(e)
	{
		var userInfo = this.$api.checkUserInfo();
		var nickname = userInfo.nickname?userInfo.nickname:'';
		var title = nickname+'推荐您使用'+this.myconfig.site_name;
		var user_id = userInfo.id?userInfo.id:0;
		var path = 'share_recom_userid='+user_id;
		var shareObj = {
			title:title,
			query:path
		}
		var shareInfo = this.shareInfo;
		if(shareInfo.imageUrl)
		{
			shareObj.imageUrl = shareInfo.imageUrl;
		}
		return shareObj;
	},
	onHide(){
		
	},
	onUnload: function () {
		
	},
}